/*
 * @Author: xiezuobing(948466)[435321508@qq.com] 
 * @Date: 2017-12-25 19:32:50 
 * @Last Modified by:   xiezuobing 
 * @Last Modified time: 2017-12-25 19:32:50 
 */
const Service = require('egg').Service;

class SourceService extends Service {
  /**
   * @description 根据id查询
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} fileId 
   * @returns  
   * @memberof SourceService
   */
  async selectById(fileId) {
    return await this.ctx.model.Resource.findById(fileId, {
        include: [
          {
            model: this.ctx.model.Folder,
            as: 'folder'
          }
        ]
      })
  }

  /**
   * @description 批量查询
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} [fileIds=[]] 
   * @returns  
   * @memberof SourceService
   */
  async selectByIds(fileIds = []) {
    return await this.ctx.model.Resource.findAll({
        where: {
          id: {
            $in: fileIds
          }
        }
      })
  }

  async list(folderId = 0, name = '', type, deleteFlag = 0) {
    const app = this.app;
    const ctx = this.ctx;
    const where = {
      folder_id: folderId,
      name: {
        $like: '%' + name + '%'
      },
      delete_flag: deleteFlag
    }
    if (type) {
      where.type = +type;
    }
    return await ctx.model.Resource.findAll({
        where: where
      });
  }

  /**
   * @description 创建资源记录
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} source 
   * @returns  
   * @memberof SourceService
   */
  async create(source) {
    return await this.ctx.model.Resource.create(source);
  }


  /**
   * @description 逻辑删除
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} values 
   * @returns  
   * @memberof SourceService
   */
  async clear(values) {
    return await this.ctx.model.Resource.update(values, {
        where: {
          id: values.id
        }
      })
  }

  /**
   * @description 更新
   * @author xiezuobing[948466]<435321508@qq.com>
   * @param {any} values 
   * @returns  
   * @memberof SourceService
   */
  async update(values) {
    return await this.ctx.model.Resource.update(values, {
        where: {
          id: values.id
        }
      })
  }



}

module.exports = SourceService;